package com.ef.web.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.ClassLevel;
import com.ef.service.ClassLevelService;
import com.ef.util.CustomExample;

public class ClassLevelAction extends BaseAction<ClassLevel> {

	private static final long serialVersionUID = 1L;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(ClassLevelAction.class);

	private ClassLevelService classLevelService;

	@SuppressWarnings("serial")
	public String list() {

		this.listResult = classLevelService.findByExample(new CustomExample<ClassLevel>(this.getModel()){
			
			public void appendCondition(Criteria criteria) {
				this.enableLike(MatchMode.ANYWHERE);
			}
		}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	public String edit() {
		
		return EDIT;
	}
	
	public String save(){
		
		ClassLevel classLevel =this.getModel();
		classLevel.setUpdateTime(new Date());
		classLevel.setUpdateUser(getSessionUser());
		classLevelService.saveOrUpdate(classLevel);
		this.setResult(ConfirmMessage.ClassLevel);
		return EDIT;
	}
	
	public String delete(){
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for(String i:id){
			classLevelService.delete(classLevelService.findById(Long.valueOf(i)));
		}
	 
		return DELETE;
	}
	
	public String same() throws IOException{
		String same = getRequest().getParameter("same");
		List<ClassLevel> list = classLevelService.findByName(same);
		String result = "{sucess:true,same:false}";
		if(list==null || list.size() == 0){
			
		}else{
			result = "{sucess:true,same:true}";
		}
		getResponse().getWriter().write(result);
		return NONE;
	}


	public ClassLevelService getClassLevelService() {
		return classLevelService;
	}

	public void setClassLevelService(ClassLevelService classLevelService) {
		this.classLevelService = classLevelService;
	}

}
